package com.weilive.pay.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.weilive.core.cache.redis.RedisConfig;
import com.weilive.core.cache.redis.RedisService;
import com.weilive.core.utils.DateUtil;
import com.weilive.pay.dao.mapper.CashTxMapper;
import com.weilive.pay.dao.model.CashTx;

/**
 * 资源
 * 
 * @author
 *
 */
@Component
public class CashTemplate {
	private static final Logger logger = LoggerFactory.getLogger(CashTemplate.class);
	@Autowired
	private RedisService redis;
	@Autowired
	private CashTxMapper cashTxDao;
	// 付费资源
	private Map<String, CashTx> cashTxMap;

	@PostConstruct
	private void init() {

		Map<String, CashTx> cashTxMap = new HashMap<String, CashTx>();
		List<CashTx> list = cashTxDao.getAllCaxh();
		for (CashTx tx : list) {
			cashTxMap.put(tx.getTxName(), tx);
		}
		setCashTxMap(cashTxMap);

		redis.exists("weilive.key.pay");
		logger.info("redis初始化成功!");
		
		String userKey = RedisConfig.USER_INFO_PREF + 535;
		if (redis.exists(userKey)) {
			Map<String, String> setMap = new HashMap<String, String>();
			setMap.put(RedisConfig.USER_VIP_EXPIRATION, DateUtil.addTime(DateUtil.MONTH, 1).getTime() / 1000L + "");
			setMap.put(RedisConfig.USER_UPDATED_TIME, DateUtil.dateFormat(new Date()));
			redis.hmset(userKey, setMap);
		}
	}

	public Map<String, CashTx> getCashTxMap() {
		return cashTxMap;
	}

	public void setCashTxMap(Map<String, CashTx> cashTxMap) {
		this.cashTxMap = cashTxMap;
	}

	public CashTx getCashTx(String id) {
		Map<String, CashTx> cashTxMap = getCashTxMap();
		if (!cashTxMap.containsKey(id)) {
			return null;
		}
		return cashTxMap.get(id);
	}
}
